import { Component ,ChangeDetectorRef} from '@angular/core';
import { IonicPage, NavController, NavParams,LoadingController } from 'ionic-angular';
import { APP_YUANCHNEG_URL } from '../../providers/Constants';
import {HttpService} from '../../providers/HttpService';
import { NativeServicetwo } from "../../providers/NativeServicetwo";
declare let cordova: any;
/**
 * Generated class for the CentrifugalPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-centrifugal',
  templateUrl: 'centrifugal.html',
})
export class CentrifugalPage {
optis=[];//接收人数组
listopt=false;//接收人列表
jieshouID='';
//转运按钮
sendOff=false;
  drawbloodshenfen=false;
  imgAll=false;
  img22=false;
  oneren=false;
  tworen=false;
avatarPath2='';
avatarPath1='';
MSG='';
  //
  all_one=true;
  token:any;
  sample=false;//接收样本div
  centrifugal=false;//离心div
  warehousing=false;//入库div
  split=false;//分装扫描
  statee: string = './assets/imgs/start.png';
  str='';
  str1='';
  str2='';
  hemolysis=false;
  S1='';
  stage='';//阶段名
  alltext=false;
  codesArrRong=[{
    id:1,
    type:1
  },{
    id:2,
    type:1
  },{
    id:3,
    type:1
  }];
  codesArr=[];
  codesArr1=[];
  codesArr2=[];
  codesArr3=[];
  allID='';
  SPID='';
  //接收
  hospital='';
  receiving='';
  //离心
  centrifugalend=false;
  rifugearr=[];
  Category=true;
  //分血结束
  split01=true;
  split02=false;

  //判断去重
  dechong=[];
  faceArr=[];
  constructor(public navCtrl: NavController,
              public navParams: NavParams,private httpService: HttpService,public cd: ChangeDetectorRef,public loadingCtrl: LoadingController,private nativeService: NativeServicetwo,){
                    this.token=this.navParams.data.value.login_token;
              }

chobxChange(row,item){
  this.cd.detectChanges(); 
  if(row.checked){
    for(var i=0;i<this.codesArrRong.length;i++){
      if(this.codesArrRong[i].id==item){
        this.codesArrRong[i].type=2;
      }
    }
  }else{
      for(var j=0;j<this.codesArrRong.length;j++){
        if(this.codesArrRong[j].id==item){
          this.codesArrRong[j].type=1;
        }
      }
  }
}
     //人脸识别按钮
            getPicture1(){
              let options = {
                 
                };
              this.nativeService.getPictureByCamera(options).then(imageBase64 => {
                  this.getPictureSuccess(imageBase64);
                  
                });
            }
             //人脸识别按钮
            getPicture2(){
              let options = {
                    
                };
              this.nativeService.getPictureByCamera(options).then(imageBase64 => {
                  this.getPictureSuccess2(imageBase64);
                  
                });
            }
            private getPictureSuccess(imageBase64) {
               
                  this.avatarPath1 = 'data:image/jpeg;base64,'+imageBase64;
                  
               
            }
            private getPictureSuccess2(imageBase64) {
               if(this.avatarPath1==''){
                  this.avatarPath1 = 'data:image/jpeg;base64,'+imageBase64;
                 
               }else{
                  this.avatarPath2 = 'data:image/jpeg;base64,'+imageBase64;
                  
                  

               }
            }
//返回
             giveup(){
                this.imgAll=false;
                this.tworen=false;
                this.oneren=false;
                this.drawbloodshenfen=true;
                this.avatarPath1 = '';
                this.avatarPath2 = '';
                this.MSG='';
                this.img22=false;

                this.cd.detectChanges(); 

             }
      getPictureGO1(){
              this.drawbloodshenfen=false;
              this.imgAll=true;
              this.oneren=true;
              this.cd.detectChanges(); 


            }
            getPictureGO2(){
              this.drawbloodshenfen=false;
              this.imgAll=true;
              this.tworen=true;
              this.img22=true;
                    this.cd.detectChanges(); 


            }
    //单人提交认证
            getPictureSub1(){
                let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });

               
                loading.present();
                let obj={
                      face1:this.avatarPath1,
                      type:1,
                      code:this.S1,
                        json:JSON.stringify(this.faceArr),
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allfaceAuth',obj).then(res=>{
                      

                      if(res.code=="111"){
                          this.tworen=false;
                          this.imgAll=false;
                          this.drawbloodshenfen=false;
                          this.oneren=false;
                           this.avatarPath1 = '';
                            this.MSG="";
                           this.alltext=false;
                           this.sample=false;
                           this.centrifugal=false;
                           this.warehousing=false;
                           this.centrifugalend=false;
                           this.drawbloodshenfen=false;
                           this.split=false;
                           this.codesArr=[];
                            this.codesArr1=[];
                            this.codesArr2=[];
                            this.codesArr3=[];
                            this.stage="";
                            this.receiving='';
                            this.hospital='';
                            this.allID='';
                            this.str1='';
                            this.str2='';
                            this.SPID='';
                            this.rifugearr=[];
                            this.all_one=true;
                            this.Category=true;
                             this.split01=true;
                             this.split02=false;
                            this.S1='';
                            this.dechong=[];
                            this.faceArr=[];
                            alert(res.msg);
                          
                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 

                        
                     })
            }

             //双人提交认证
            getPictureSub2(){
                let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });

               
                loading.present();
                let obj={
                      face1:this.avatarPath1,
                      face2:this.avatarPath2,
                      type:2,
                      json:JSON.stringify(this.faceArr),
                      code:this.S1,
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allfaceAuth',obj).then(res=>{
                      

                      if(res.code=="111"){

                          this.tworen=false;
                          this.imgAll=false;
                          this.drawbloodshenfen=false;
                          this.oneren=false;
                           this.avatarPath1 = '';
                            this.MSG="";
                           this.alltext=false;
                           this.sample=false;
                           this.centrifugal=false;
                           this.warehousing=false;
                           this.centrifugalend=false;
                           this.drawbloodshenfen=false;
                           this.split=false;
                           this.codesArr=[];
                            this.codesArr1=[];
                            this.codesArr2=[];
                            this.codesArr3=[];
                            this.stage="";
                            this.receiving='';
                            this.hospital='';
                            this.allID='';
                            this.str1='';
                            this.str2='';
                            this.SPID='';
                            this.rifugearr=[];
                            this.all_one=true;
                            this.Category=true;
                             this.split01=true;
                             this.split02=false;
                            this.S1='';
                            this.faceArr=[]; 
                            this.dechong=[];
                            alert(res.msg);
                          
                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }else if(res.value=='2'){
                              this.avatarPath2="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 

                        
                     })
            }

    /////////////////////////////////
      all(){
        cordova.plugins.test.star(result=>{
                  this.str=result;
                  this.S1=this.str.substr(0, 1);

                      var obj={
                          code:this.str,
                          login_token:this.token
                      }
                    if(this.S1=="C"){//分血
                          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/splitBlood',obj).then(res=>{
                              
                             if(res.code=="111"){
                                  this.str1=this.str;
                                  this.faceArr.push(this.str);
                                 this.all_one=false;
                                 let oob={
                                  num:'',
                                  random:'',
                                  ppname:''
                                 };
                                 oob.num=res.value.columns.num;
                                 oob.random=res.value.columns.random_num;
                                 oob.ppname=res.value.columns.ppname;
                                this.codesArr.push(oob);
                                  this.hospital=res.value.columns.sname;
                                  this.stage="分装操作";
                                  this.SPID=res.value.columns.spid;
                                  this.split=true;
                                  this.alltext=true;
                                  this.Category=false;
                                  this.cd.detectChanges();  
                             }else{
                                  alert(res.msg);
                             }
                            
                              
                           })
                    }else if(this.S1=="B"){//冻存
                          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/parCode',obj).then(res=>{
                         
                             if(res.code=="111"){
                                  this.all_one=false;
                                  
                                  this.stage="入库操作";
                                  this.receiving=res.value.columns.dname;
                                  this.hospital=res.value.columns.sname;
                                  this.allID=res.value.columns.id;
                                  this.alltext=true;
                                  this.warehousing=true;
                                  
                                    
                                 
                                  this.cd.detectChanges();  
                             }else{
                                  alert(res.msg);
                             }
                              
                           })


                    }else if(this.S1=="F"){//接收
                          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/getUers',{login_token:this.token}).then(res=>{
                             if(res.code=="111"){
                              this.listopt=true;
                              this.sendOff=true;
                              this.all_one=false;
                                 this.optis=res.value;
                                  
                                  this.cd.detectChanges();  
                             }else{
                                  alert(res.msg);
                             }
                              
                           })
                        
                    }else if(this.S1=="L"){//离心
                        this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/parCode',obj).then(res=>{
                         
                             if(res.code=="111"){
                                  this.all_one=false;
                                  
                                  this.stage="离心开始";
                                  this.receiving=res.value.columns.dname;
                                  this.hospital=res.value.columns.sname;
                                  this.allID=res.value.columns.id;
                                  this.alltext=true;
                                  this.centrifugal=true;
                                 
                                  this.cd.detectChanges();  
                             }else if(res.code=="222"){
                                  
                                  this.codesArr=res.value;
                                  for(var i=0;i<res.value.length;i++){
                                      this.rifugearr.push(res.value[i].id);
                                      let rongObj={
                                        id:res.value[i].id,
                                        type:1
                                      }
                                      this.codesArrRong.push(rongObj);
                                    }
                                    
                                  this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/parCodeL',obj).then(res=>{
                               
                                     if(res.code=="111"){
                                          this.stage="离心结束";
                                          this.receiving=res.value.columns.dname;
                                          this.hospital=res.value.columns.sname;
                                          this.allID=res.value.columns.id;
                                          this.all_one=false;
                                            this.hemolysis=true;
                                  
                                          this.alltext=true;
                                          this.centrifugalend=true;
                                          this.cd.detectChanges(); 
                                      }else{
                                        alert(res.msg);
                                      }
                               
                                      
                                  })

                                  
                             }else{
                                  alert(res.msg);
                             }
                              
                           })
                    }else{
                      alert("请正确扫描");
                    }



                      
                  
                  
              },error=>console.log(error));
      }
//确定人转运人
sendBtnQD(){
var obj={
              code:this.str,
              login_token:this.token
          }
  this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/parCode',obj).then(res=>{
                         
                             if(res.code=="111"){
                                  this.all_one=false;
                                  this.listopt=false;
                                  this.sendOff=false;

                                  this.stage="接收操作";
                                  this.receiving=res.value.columns.dname;
                                  this.hospital=res.value.columns.sname;
                                  this.allID=res.value.columns.id;
                                  this.alltext=true;
                                  this.sample=true;
                                  
                                  this.cd.detectChanges();  
                             }else{
                                  alert(res.msg);
                             }
                              
                           })
}
//取消转运人
sendBtnQX(){
  this.all_one=true;
  this.listopt=false;
  this.sendOff=false;
  this.jieshouID='';
  this.cd.detectChanges();  

}
      //接收扫描
      receiveScan(){
        cordova.plugins.test.star(result=>{
            this.str=result;
            let obj={
                              code:this.str,
                              login_token:this.token
                          }
             this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/message',obj).then(res=>{
                             
                                 if(res.code=="111"||res.code=="222"){
                                        for(var i=0;i<this.codesArr1.length;i++){
                                            if(this.codesArr1[i]==this.str){
                                                alert("该采血管已扫描！");
                                                return;
                                            }
                                            
                                        }
                                      let oob={
                                        num:'',
                                        random:'',
                                        ppname:''
                                       };
                                       oob.num=res.value.columns.num;
                                       oob.random=res.value.columns.random_num;
                                       oob.ppname=res.value.columns.ppname;
                                       this.codesArr.push(oob);
                                      this.codesArr1.push(this.str);
                                      
                                      //this.codesArr1.push(res.value.columns.spid);
                                      //this.codesArr2.push(res.value.columns.sid);
                                      this.cd.detectChanges();  
                                      if(res.code=='222'){
                                        alert(res.msg);
                                      }
                                 }else{
                                    alert(res.msg);
                                 }
                                      
                                
                                  
                               })
         },error=>console.log(error));
      }
      //一键接收
      receive(){
        let obj={ 
          json:JSON.stringify(this.codesArr1),
          //json2:JSON.stringify(this.codesArr2),
           code:this.allID,
           name:this.jieshouID,
          login_token:this.token
          }
        this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/receiveSample',obj).then(res=>{
                         
                             if(res.code=="111"){
                             this.listopt=false;
                                  this.drawbloodshenfen=true;
                                  this.alltext=false;
                                  this.sample=false;
                                  this.faceArr=this.codesArr1;
                                 
                                 
                                  this.cd.detectChanges();  
                             }else{
                             
                             }
                              alert(res.msg);
                              
                           })
      }
      //离心扫描
      scanning(){
        cordova.plugins.test.star(result=>{
            this.str=result;
            let obj={
                    code:this.str,
                    login_token:this.token
                }
            this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/message01',obj).then(res=>{
                             
                                 if(res.code=="111"){
                                      for(var i=0;i<this.dechong.length;i++){
                                            if(this.dechong[i]==this.str){
                                                alert("该采血管已扫描！");
                                                return;
                                            }
                                            
                                        }
                                      let oob={
                                        num:'',
                                        random:'',
                                        ppname:''
                                       };
                                       oob.num=res.value.columns.num;
                                       oob.random=res.value.columns.random_num;
                                       oob.ppname=res.value.columns.ppname;
                                    this.codesArr.push(oob);
                                    this.dechong.push(this.str);
                                      this.codesArr1.push(res.value.columns.spid);
                                      this.codesArr2.push(res.value.columns.sid);
                                     
                                      this.cd.detectChanges();  
                                 }else{
                                  alert(res.msg);
                                 }
                                      
                                 
                                  
                               })
         },error=>console.log(error));
      }
      //一键离心
      oneBondcen(){
          let obj={
              json:JSON.stringify(this.codesArr1),
              json2:JSON.stringify(this.codesArr2),
               code:this.allID,
              login_token:this.token
              }
          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/receiveCentrifuge',obj).then(res=>{
                         
                             if(res.code=="111"){
                                  this.drawbloodshenfen=true;
                                  this.alltext=false;
                                  this.centrifugal=false;
                                  this.faceArr=this.dechong;
                                 
                                 
                                  this.cd.detectChanges();  
                             }else{
                                  
                                 }
                             alert(res.msg);
                              
                           })
      }
      //冻存扫描
      warehoScan(){
      cordova.plugins.test.star(result=>{
            this.str=result;
              let obj={
                        code:this.str,
                        login_token:this.token
                            }
              this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/iceBoxCode',obj).then(res=>{
                               
                             if(res.code=="111"){
                                  for(var i=0;i<this.dechong.length;i++){
                                            if(this.dechong[i]==this.str){
                                                alert("该采血管已扫描！");
                                                return;
                                            }
                                            
                                        }
                                   let oob={
                                        num:'',
                                        random:'',
                                        ppname:''
                                       };
                                       oob.num=res.value.columns.num;
                                       oob.random=res.value.columns.random_num;
                                       oob.ppname=res.value.columns.ppname;
                                        this.codesArr.push(oob);
                                        this.dechong.push(this.str);
                                        this.codesArr1.push(res.value.columns.spid);
                                        this.codesArr2.push(res.value.columns.sid);
                                 
                                      this.codesArr3.push(res.value.columns.ppname);
                                      this.cd.detectChanges();  
                             }else{
                                  alert(res.msg);
                                 }
                             
                              
                           })
            },error=>console.log(error));
      }
      //一键冻存
      oneKeyinto(){
          let obj={
              json:JSON.stringify(this.codesArr1),
              json2:JSON.stringify(this.codesArr2),
              json3:JSON.stringify(this.codesArr3),
               code:this.allID,
              login_token:this.token
            }
          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/iceBox',obj).then(res=>{
                         
                             if(res.code=="111"){
                                    this.drawbloodshenfen=true;
                                  this.alltext=false;
                                
                                 this.warehousing=false;
                                 this.faceArr=this.dechong;
                                 
                                  this.cd.detectChanges();  
                             }else{
                                 
                                 }
                              alert(res.msg);
                              
                           })
      }
      //分装操作
      splitScan(){
        cordova.plugins.test.star(result=>{
            this.str2=result;
          
            
           let obj={
                  spid:this.SPID,
                  code:this.str2,
                  login_token:this.token
              }
            
              
              this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/splitBlood01',obj).then(res=>{
                               
                         if(res.code=="111"){
                            this.split01=false;
                            this.split02=true;
                            let oob={
                                  num:'',
                                  random:'',
                                  ppname:''
                                 };
                                 oob.num=res.value.columns.num;
                                 oob.random=res.value.columns.random_num;
                                 oob.ppname=res.value.columns.ppname;
                                this.codesArr.push(oob);


                            
                            this.cd.detectChanges();  
                         }else{
                                  alert(res.msg);
                                 }
                          
                       })
            },error=>console.log(error));
      }
      splitScan2(){
           let obj={
                  spid:this.SPID,
                  code1:this.str1,
                  code2:this.str2,
                  login_token:this.token
              }
              this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/splitBloodDate',obj).then(res=>{
                               
                         if(res.code=="111"){
                          
                          this.alltext=false;
                            this.split02=false;
                            
                             this.split=false;
                             this.drawbloodshenfen=true;
                              this.split01=true;
                              this.Category=true;
                              this.str1='';
                              this.str2='';

                            
                              
                              
                            this.cd.detectChanges();  
                         }else{
                                  
                                 }
                         alert(res.msg);
                          
                       })
      }
      //重新离心
      recentr(){
          let obj={
              json2:JSON.stringify(this.rifugearr),
              login_token:this.token
          }
          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/newReceive',obj).then(res=>{
                               
                 if(res.code=="111"){
                 this.centrifugalend=false;
                 this.alltext=false;
                 this.drawbloodshenfen=true;
                 this.hemolysis=false;
                     this.rifugearr=[];
                        this.codesArrRong=[];
                    this.cd.detectChanges();  
                 }else{
                      
                  }
                  alert(res.msg);
                 
                  
               })
    }
      //离心结束
      ifugalEnd(){
          let obj={
              json2:JSON.stringify(this.codesArrRong),
              login_token:this.token
          }
          this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/endReceive',obj).then(res=>{
                               
                   if(res.code=="111"){
                       this.centrifugalend=false; 
                       this.alltext=false;
                       this.hemolysis=false;
                        this.rifugearr=[];
                        this.codesArrRong=[];
                    this.drawbloodshenfen=true;

                    
                      this.cd.detectChanges();  
                   }else{
                          
                         }
                   alert(res.msg);
                    
                 })
      }

      //放弃认证
      gevidence(){


          this.alltext=false;
         this.sample=false;
         this.centrifugal=false;
         this.warehousing=false;
         this.centrifugalend=false;
         this.drawbloodshenfen=false;
         this.split=false;
         this.codesArr=[];
          this.codesArr1=[];
          this.codesArr2=[];
          this.codesArr3=[];
          this.stage="";
          this.receiving='';
          this.hospital='';
          this.allID='';
          this.str1='';
          this.str2='';
          this.SPID='';
          this.rifugearr=[];
          this.faceArr=[]; 
          this.all_one=true;
          this.Category=true;
           this.split01=true;
           this.split02=false;
          this.S1='';
          this.dechong=[];
          this.MSG="";
          this.listopt=false;
  this.sendOff=false;
  this.jieshouID='';

          this.cd.detectChanges(); 
      }
      //重置
      reset(){
      this.listopt=false;
  this.sendOff=false;
      this.listopt=false;
      this.hemolysis=false;
       this.alltext=false;
       this.sample=false;
       this.centrifugal=false;
       this.warehousing=false;
       this.centrifugalend=false;
       this.drawbloodshenfen=false;
       this.split=false;
       this.codesArr=[];
        this.codesArr1=[];
        this.codesArr2=[];
        this.codesArr3=[];
        this.stage="";
        this.receiving='';
        this.hospital='';
        this.allID='';
        this.str1='';
        this.str2='';
        this.SPID='';
        this.rifugearr=[];
        this.all_one=true;
        this.Category=true;
         this.split01=true;
          this.dechong=[];
         this.split02=false;
         this.faceArr=[]; 
        this.S1='';
        this.jieshouID='';
        this.MSG="";
        this.cd.detectChanges(); 
      }
      

}
